layui.use(['admin', 'form', 'upload'], function () {
    let $ = layui.$
        , admin = layui.admin
        , setter = layui.setter
		, upload = layui.upload
        , form = layui.form;

    let layerIndex = '';
    layui.data.sendParams = function (params) {
        layerIndex = params.lay_index;
    }
	let token = layui.data(setter.tableName)[setter.request.tokenName];
	let tokenName = setter.request.tokenName;
	setTimeout(function () {
	    let uploadInst = upload.render({
	        elem: '#commodity-upload-normal'
	        ,url: setter.request.domainName + '/admin/Ajax/upload?server=true&' + tokenName + '=' + token
	        ,accept: 'images'
	        ,acceptMime: 'image/jpeg,image/png,image/jpg'
	        ,exts: 'jpeg|jpg|png'
	        ,size: setter.maxFileSize * 1024
	        ,before: function(obj){
	            // 预读本地文件示例，不支持ie8
	            // obj.preview(function(index, file, result){
	            //     $('#upload-normal-img').attr('src', result); //图片链接（base64）
	            // });
	        }
	        ,done: function(res){
	            //如果上传失败
	            if(res.code === 1){
	                let full_url = res.data.file.full_url;
	                let url = res.data.file.url;
	                $('#commodity-upload-normal-img').attr('src', full_url);
	                form.val("commodityPopup", {
	                    "img": url,
	                });
	                return layer.msg('上传成功！');
	            }else{
	                return layer.msg('上传失败！');
	            }
	            //上传成功
	        }
	        ,error: function(){
	            //演示失败状态，并实现重传
	            // uploadInst.upload(); // 重新上传
	        }
	    });
		$(document).on('click', '.img-show', function (e) {
		    let src = $(this).attr("src");
		    layer.photos({
		        photos: {
		            "title": "Photos",
		            "start": 0,
		            "data": [
		                {
		                    "alt": "",
		                    "pid": 1,
		                    "src": src,
		                }
		            ]
		        },
		        footer: false // 是否显示底部栏 --- 2.8.16+
		    });
		});
	}, 520);
    // 新增
    form.on('submit(laySubmitAdd)', function(data){
        let field = data.field; //获取提交的字段
        // 提交 Ajax 成功后，关闭当前弹层并重载表格
        admin.req({
            url: setter.request.domainName + '/admin/commodity.index/add' //实际使用请改成服务端真实接口
            , data: field
            , type: 'POST'
            , loading: true // 是否开启加载框
            , done: function(res){
                admin.message({
                    title: '提交成功'
                    ,offset: '15px'
                    ,icon: 1
                    ,time: 1500
                })
                layui.table.reload('commodityTable'); //重载表格
                layer.close(layerIndex); //执行关闭
            }
        })
    });
    // 开启提交
    form.on('submit(laySubmitEdit)', function (data) {
        let field = data.field; //获取提交的字段
        admin.req({
            url: setter.request.domainName + '/admin/commodity.index/edit'
            , data: field
            , type: 'post'
            , loading: true // 是否开启加载框
            , done: function(res){
                admin.message({
                    title: '提交成功'
                    ,offset: '15px'
                    ,icon: 1
                    ,time: 1500
                })
                layui.table.reload('commodityTable'); //重载表格
                layer.close(layerIndex); //执行关闭
            }
        });
    });

})
